package com.aos.erp.manager.service.impl;

import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.manager.dto.NoticeDTO;
import com.aos.erp.manager.dto.NoticeDetailDTO;
import com.aos.erp.manager.enums.DelStatus;
import com.aos.erp.manager.enums.ReadStatus;
import com.aos.erp.manager.mapper.notice.NoticeDispenseOrgMapper;
import com.aos.erp.manager.mapper.notice.NoticeMapper;
import com.aos.erp.manager.model.notice.vo.NoticeConditionVo;
import com.aos.erp.manager.model.notice.vo.NoticeVo;
import com.aos.erp.manager.model.notice.NoticeDispenseOrg;
import com.aos.erp.manager.model.notice.NoticeInfo;
import com.aos.erp.manager.service.NoticeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, NoticeInfo> implements NoticeService {

    @Resource
    private NoticeDispenseOrgMapper dispenseOrgMapper;

    @Override
    public void saveNotice(NoticeVo dataInfo) throws Exception {

        checkSaveNoticeData(dataInfo);

        // 保存新公告：公告标题、公告内容、公告类型、弹窗时间，获得公告ID
        long noticeId = saveNoticeData(dataInfo);

        // 保存下发组织
        saveDispenseOrg(noticeId, dataInfo.getDispenseOrgList());
    }

    @Override
    public void deleteNotice(String dataId) throws Exception {

        if(dataId.length() == 0) {
            throw new BizException("公告ID不能为空");
        }
        baseMapper.deleteNotice(dataId);
    }

    @Override
    public void updateNotice(NoticeVo dataInfo) throws Exception {

        checkUpdateNoticeData(dataInfo);

        // 修改通知信息
        baseMapper.updateNotice(dataInfo);
        long noticeId = dataInfo.getId();

        // 保存下发组织
        // 删除旧记录
        Condition condition = new Condition(NoticeDispenseOrg.class);
        condition.eqFiled("noticeId", noticeId);
        dispenseOrgMapper.delete(condition.getWrapper());

        // 保存新记录
        saveDispenseOrg(noticeId, dataInfo.getDispenseOrgList());
    }

    @Override
    public NoticeInfo getNoticeById(long dataId) {
        return baseMapper.selectById(dataId);
    }

    @Override
    public NoticeDTO getNoticeDetail(long dataId) {

        // 获取公告详情
        NoticeInfo noticeInfo = baseMapper.selectById(dataId);
        // 获取公告下发组织列表
        Condition condition = new Condition(NoticeDispenseOrg.class);
        condition.eqFiled("noticeId", dataId);
        List<NoticeDispenseOrg> dispenseOrgList = dispenseOrgMapper.selectList(condition.getWrapper());

        NoticeDTO noticeDTO = new NoticeDTO();
        noticeDTO.setNoticeData(noticeInfo);
        noticeDTO.setDispenseOrgList(dispenseOrgList);
        return noticeDTO;
    }

    /**
     * 公告列表分页查询
     * @param conditionVo
     * @return
     */
    @Override
    public PageBean<NoticeDetailDTO> findByPage(NoticeConditionVo conditionVo) {
        Page<NoticeDetailDTO> page = new Page<NoticeDetailDTO>(conditionVo.getPageIndex(), conditionVo.getPageSize());
        List<NoticeDetailDTO> noticeInfoList = baseMapper.pageNoticeList(page, conditionVo);
        page.setRecords(noticeInfoList);
        
        PageBean<NoticeDetailDTO> bean = new PageBean<NoticeDetailDTO>(page.getCurrent(), page.getSize(), page.getTotal());
        bean.setOptimizeCountSql(page.optimizeCountSql());
        bean.setAscs(page.ascs());
        bean.setDescs(page.descs());
        bean.setRecords(page.getRecords());
        bean.condition();

        return bean;
    }

    /**
     * 保存通知信息
     * @param dataInfo
     * @return
     */
    private long saveNoticeData(NoticeVo dataInfo) {

        Date nowDate = new Date();
        NoticeInfo noticeData = new NoticeInfo();
        BeanUtils.copyProperties(dataInfo, noticeData);
        noticeData.setIsDel(DelStatus.del_no);
//        noticeData.setTipsStartTime(noticeData.getTipsStartTime());
//        noticeData.setTipsEndTime(noticeData.getTipsEndTime());
//        noticeData.setNoticeType(dataInfo.getNoticeType());
//        noticeData.setNoticeTitle(dataInfo.getNoticeTitle());
//        noticeData.setNoticeContent(dataInfo.getNoticeContent());
        noticeData.setCreateUser(dataInfo.getOptUser());
        noticeData.setCreateTime(nowDate);
        noticeData.setUpdateUser(dataInfo.getOptUser());
        noticeData.setUpdateTime(nowDate);
        this.save(noticeData);
        return noticeData.getId();
    }

    /**
     * 保存通知下发组织
     * @param noticeId
     * @param dispenseOrgList
     */
    private void saveDispenseOrg(long noticeId, List<NoticeDispenseOrg> dispenseOrgList) {
        for (NoticeDispenseOrg dispenseOrg : dispenseOrgList) {
            dispenseOrg.setNoticeId(noticeId);
            dispenseOrg.setReadStatus(ReadStatus.read_no);
            dispenseOrgMapper.insert(dispenseOrg);
        }
    }

    private void checkNoticeData(NoticeVo params) throws Exception {
        if(params.getNoticeTitle().isEmpty()) {
            throw new BizException("公告标题不能为空");
        }
        if(params.getNoticeTitle().length() > 100) {
            throw new BizException("公告标题长度不能超过100");
        }
        if(params.getNoticeContent().isEmpty()) {
            throw new BizException("公告内容不能为空");
        }
        if(params.getNoticeContent().length() > 5000) {
            throw new BizException("公告内容长度不能超过5000");
        }
        if(params.getDispenseOrgList().size() == 0) {
            throw new BizException("公告下发组织不能为空");
        }
        if(params.getDispenseOrgList().size() > 16) {
            throw new BizException("公告下发组织不能超过16个");
        }
    }

    private void checkSaveNoticeData(NoticeVo params) throws Exception {
        checkNoticeData(params);
    }

    private void checkUpdateNoticeData(NoticeVo params) throws Exception {

        if(params.getId() == 0) {
            throw new BizException("公告ID不能为空");
        }
        checkNoticeData(params);
    }
}
